package jlib.component.tool {
	
	import jlib.component.tool.catchers.ICatcher;
	import jlib.component.tool.factory.DefaultSelectToolFactory;
	import jlib.component.tool.factory.ISelectToolFactory;
	
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;

	/**
	 * @author 박종희
	 */
	public class SelectTool implements ITool, ISelectableTool
	{
		public static const CATCH_SINGLE:String = "catchSingle";
		public static const CATCH_MULTI:String = "catchMulti";
		public static const CATCHED_NEWSINGLE:String = "cathcedNewSingle";
		public static const CATCHED_OVERLAP:String = "catchedOverlap";
		
		private var catcher:ICatcher;
		private var selectBox:DragSelectBox;
		private var stage:Stage;
		private var factory:ISelectToolFactory;
		
		public function SelectTool($factory:ISelectToolFactory=null): void
		{
			if($factory == null) this.factory = new DefaultSelectToolFactory();
			else this.factory = factory;
			initialize();
		}
		
		public function initialize(): void
		{
			catcher = factory.createCatcher();
			selectBox = new DragSelectBox();
		}
		
		// document 확장
		private function mouseHandler( e:MouseEvent ): void
		{
			switch( e.type ){
				case MouseEvent.MOUSE_DOWN :
					catcher.press(e);
					selectBox.press(e);
					stage.addEventListener( MouseEvent.MOUSE_MOVE , mouseHandler );
					stage.addEventListener( MouseEvent.MOUSE_UP , mouseHandler );
					break;
				case MouseEvent.MOUSE_MOVE :
					catcher.move(e);
					selectBox.move(e);
					break;
				case MouseEvent.MOUSE_UP :
					catcher.release(e);
					selectBox.release(e);
					stage.removeEventListener( MouseEvent.MOUSE_MOVE , mouseHandler );
					stage.removeEventListener( MouseEvent.MOUSE_UP , mouseHandler );
					break;
			}
		}
		
		// 확장 도큐맨트;
		public function setCanvas(canvas:DisplayObjectContainer): void
		{
			_setElements(canvas);
		}
		
		protected function _setElements(canvas:DisplayObjectContainer): void
		{
			stage = canvas.stage;
			catcher.setCanvas( canvas );
			catcher.setStage( stage );
			selectBox.setTarget( canvas );
			canvas.parent.addChild(selectBox);
		}
		
		public function on(): void { stage.addEventListener( MouseEvent.MOUSE_DOWN, mouseHandler ); }
		public function off(): void { stage.removeEventListener( MouseEvent.MOUSE_DOWN, mouseHandler ); }
		
		public function setSelectList($list:Array): void { catcher.setCatchedList($list); }
		public function getSelectList() : Array { return catcher.getCatchedList(); }
		
		public function update( e:Event = null ) : void { }
		
		public function dispatchEvent(event : Event) : Boolean  { return catcher.dispatchEvent(event);	}
		public function hasEventListener(type : String) : Boolean { return catcher.hasEventListener(type); }
		public function willTrigger(type : String) : Boolean { return catcher.willTrigger(type); }
		public function removeEventListener(type : String, listener : Function, useCapture : Boolean = false) : void { catcher.removeEventListener(type, listener);	 }
		public function addEventListener( type:String, listener: Function, useCapture : Boolean = false, priority : int = 0, useWeakReference : Boolean = false) : void { catcher.addEventListener(type,listener,useCapture,priority,useWeakReference);	 }
		
	}//c
}//p

